Skip to main content

Search in Rotated Sorted Array II

Question

Given an integer array nums sorted in ascending order, and an integer target, write a function to determine if the target is present in the array, with the possibility of duplicates. The array may be rotated at some unknown pivot.

Example 1
Input: [2,5,6,0,0,1,2], target = 0
Output: true

Solution

all//Search in Rotated Sorted Array II.py


class Solution:
def search(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] == target:
return True
if nums[mid] > nums[right]:
if target < nums[mid] and target >= nums[left]:
right = mid - 1
else:
left = mid + 1
elif nums[mid] < nums[right]:
if target > nums[mid] and target <= nums[right]:
left = mid + 1
else:
right = mid - 1
else:
right -= 1
return False